﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;

namespace Bon.It.Server
{
    public partial class Server : ServiceBase
    {
        internal static ServiceHost _ServiceHost;

        public Server()
        {
            InitializeComponent();
            _ServiceHost = new ServiceHost(typeof(ContractManager));
            ServiceBehaviorAttribute behavior = _ServiceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
            behavior.UseSynchronizationContext = false; 


        }

        public void StartService()
        {
            Debug.Assert(_ServiceHost != null, "Servicehost wasn't initialized");
            
            _ServiceHost.Open();
        }


        public void StopService()
        {
            Debug.Assert(_ServiceHost != null, "Servicehost wasn't initialized");

            if (_ServiceHost.State != CommunicationState.Opened)
            {
                _ServiceHost.Close();
                _ServiceHost = null;
            }
        }

        protected override void OnStart(string[] args)
        {
            StartService();
        }

        /// <summary>
        /// Closing the channel.
        /// </summary>
        protected override void OnStop()
        {

            StopService();

        }
    }
}
